package thread2;

import java.util.concurrent.TimeUnit;

public class ThreadDemo4 implements Runnable {
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println("thread is running" + i++);
        }
    }

    public void stop() {
        this.flag = false;
    }

    public static void main(String[] args) {
        ThreadDemo4 demo4 = new ThreadDemo4();
        Thread thread = new Thread(demo4);
        thread.start();

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 1000; i++) {
            if (i == 900) {
                demo4.stop();
                System.out.println("stop");
            }
        }
    }
}
